package com.sfx.concurrent.demo.concurrent.ExcutorService;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-26
 * Time: 15:16
 */
@Slf4j(topic = "c.FiexedPool")
public class FiexedPool {

    public static void main1(String[] args) {
        ExecutorService service =
                Executors.newFixedThreadPool(2,
                        new ThreadFactory() {
                            private AtomicInteger t = new AtomicInteger(1);
                            @Override
                            public Thread newThread(Runnable r) {
                                return new Thread(r,"mypool-"+t.getAndIncrement());
                            }
                        });
        service.execute(()->{
            log.debug("1");
        });
        service.execute(()->{
            log.debug("2");
        });
        service.execute(()->{
            log.debug("3");
        });
    }

    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(1, new ThreadFactory() {
            /**
             * 线程工厂为线程池起一个有意义的名字
             */
            private AtomicInteger atomicInteger = new AtomicInteger(1);
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r,"MyPool-"+atomicInteger.getAndIncrement()+"");
            }
        });
        pool.execute(()->{
            log.debug("1");
        });
        pool.execute(()->{
            log.debug("2");
        });
        pool.execute(()->{
            log.debug("3");
        });
        log.debug("start");
    }

}
